package com.liu.socket;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

@SuppressWarnings({"all"})
public class SocketTcpClient03 {
    public static void main(String[] args) throws IOException {
        //链接服务器
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        System.out.println("链接成功");
        System.out.println(socket.getClass());
        //得到和socket关联的输出流对象
        OutputStream outputStream = socket.getOutputStream();
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
        bufferedWriter.write("hello,server,字符流传输练习");
        //插入换行符，表示写入内容结束,要求对方采用readLine()来读，才能起作用
        bufferedWriter.newLine();
        //需要手动刷新，否则数据不会给通道
        bufferedWriter.flush();

        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String s = bufferedReader.readLine();
        System.out.println(s);
        //关闭流和socket
        outputStream.close();
        socket.close();
        bufferedWriter.close();
        inputStream.close();
        bufferedReader.close();
        System.out.println("客户端退出");
    }
}
